﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Sunboxsoft.WinForm;
using DevExpress.XtraEditors;
using DevExpress.XtraGrid.Views.Grid;
using WebRef = Sunboxsoft.ASMS.Client.Domain.WebRef;
using Service = Sunboxsoft.ASMS.Client.Domain.Service;
using Sunboxsoft.Common;
using Sunboxsoft.ASMS.Client.UI.Properties;
namespace Sunboxsoft.ASMS.Client.UI.Booking
{
    /// <summary>
    /// 客户信息列表
    /// </summary>
    public partial class SelectCustomerView : UserControl, IView
    {
        //当前页  
        private int _pageIndex = 0;
        //总共几页
        private int _pageCount = 0;

        Promise _promise = new Promise();

        public SelectCustomerView()
        {
            InitializeComponent();
            this.ctlGrid.EmbeddedNavigator.ButtonClick += new DevExpress.XtraEditors.NavigatorButtonClickEventHandler(EmbeddedNavigator_ButtonClick);
        }

        #region IView 成员
        public void RegisterAction(ActionNodeCollection collection)
        {
            collection.AddAction(new ActionNode("back", Resources.isBack, "返回", ctlBtnCancel_ItemClick))
                .AddAction(new ActionNode("create", Resources.isCreateCustomer, "新增", ctlBtnCreate_ItemClick))
                .AddAction(new ActionNode("update", Resources.isEdit, "修改", ctlBtnEdit_ItemClick))
                .AddAction(new ActionNode("refresh", Resources.isRefresh, "刷新", btnRefresh_ItemClick))
                .AddAction(new ActionNode("next", Resources.isNextStep, "下一步", ctlBtnNext_ItemClick));
        }
        public void Active(ViewActiveArgs args)
        {
            _promise
                .Then(RefreshData)
                .Run();
        }
        public void Resume(ViewResumeArgs args)
        {
            if (args.ClosedArgs.Result)
            {
                if (args.ClosedArgs.View is SelectAutomobileView)
                {
                    UIContext.Current.ViewManager.CloseView(true, null);
                }
                else
                {
                    RefreshData();
                }
            }
        }
        public ViewHeader GetHeader()
        {
            return new ViewHeader("客户信息列表");
        }
        #endregion

        #region Method
        private int GetPageLimit()
        {
            return 25;
        }
        /// <summary>
        /// 数据绑定
        /// </summary>
        private void RefreshData()
        {
            this.ShowMask("loading...");
            UIContext.Current.InvokeAsync(() =>
            {
                string filter = txtQry.EditValue == null ? null : txtQry.EditValue.ToString();
                PagedResult<WebRef.VCustomer> result = new Service.QueryService().QueryCustomerPagedList(filter, _pageIndex * GetPageLimit(), GetPageLimit());
                this.BeginInvoke(new Action(() =>
                {
                    if (result.IsSuccess)
                    {
                        this.ctlGrid.Paginate<WebRef.VCustomer>(result,
                           out _pageIndex,
                           out _pageCount);
                        _promise.Resolve();
                    }
                    else
                    {
                        UIContext.Current.ViewManager.ShowError(result.Message);
                        _promise.Reject();
                    }
                    this.HideMask();
                }));
            });
        }
        #endregion

        #region Action
        /// <summary>
        /// 返回
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ctlBtnCancel_ItemClick(object sender, object e)
        {
            UIContext.Current.ViewManager.CloseView(false, null);
        }
        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ctlBtnCreate_ItemClick(object sender, object e)
        {
            UIContext.Current.ViewManager.PopupView(new Create.CreateCustomerView(), new PopupViewArgs("新增客户"));
        }
        /// <summary>
        /// 修改
        /// </summary>
        private void ctlBtnEdit_ItemClick(object sender, object e)
        {
            if (this.GVData.FocusedRowHandle > -1)
            {
                WebRef.VCustomer item = this.GVData.GetFocusedRow() as WebRef.VCustomer;
                if (item != null)
                {
                    UIContext.Current.ViewManager.PopupView(new Edit.UpdateCustomerView(item.Id), new PopupViewArgs("修改客户信息"));
                }
            }
        }
        /// <summary>
        /// 刷新
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnRefresh_ItemClick(object sender, object e)
        {
            _pageIndex = 0;
            RefreshData();
        }
        /// <summary>
        /// 下一步
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ctlBtnNext_ItemClick(object sender, object e)
        {
            if (this.GVData.FocusedRowHandle > -1)
            {
                WebRef.VCustomer entity = GVData.GetRow(GVData.FocusedRowHandle) as WebRef.VCustomer;
                UIContext.Current.ViewManager.SwitchView(new Booking.SelectAutomobileView(entity));
            }
        }
        void EmbeddedNavigator_ButtonClick(object sender, DevExpress.XtraEditors.NavigatorButtonClickEventArgs e)
        {
            if (e.UpdatePageIndex(_pageCount, ref _pageIndex))
            {
                RefreshData();
            }
        }
        /// <summary>
        /// 勾选记录
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void RICheckEditSelect_Click(object sender, EventArgs e)
        {
            //    ((DevExpress.XtraEditors.CheckEdit)(sender)).EditValue = true;
            //    ((DevExpress.XtraEditors.CheckEdit)(sender)).Checked = true;
            //    ((DevExpress.XtraEditors.CheckEdit)(sender)).CheckState = CheckState.Checked;
        }
        #endregion
    }
}
